package com.quanxiaoha.weblog.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanxiaoha.weblog.admin.model.vo.category.AddCategoryReqVO;
import com.quanxiaoha.weblog.admin.model.vo.category.DeleteCategoryReqVO;
import com.quanxiaoha.weblog.admin.model.vo.category.FindCategoryPageListRspVO;
import com.quanxiaoha.weblog.admin.model.vo.category.GetCategoryPageListReqVO;
import com.quanxiaoha.weblog.admin.service.AdminCategoryService;
import com.quanxiaoha.weblog.common.domain.dos.CategoryDO;
import com.quanxiaoha.weblog.common.domain.mapper.CategoryMapper;
import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum;
import com.quanxiaoha.weblog.common.exception.BizException;
import com.quanxiaoha.weblog.common.model.vo.SelectRspVO;
import com.quanxiaoha.weblog.common.utils.PageResponse;
import com.quanxiaoha.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public Response addCategory(AddCategoryReqVO addCategoryReqVO) {

        //取出对应的分类名
        String categoryName = addCategoryReqVO.getName();

        //判断分类名是否已存在
        CategoryDO categoryDO = categoryMapper.selectByName(categoryName);
        if (Objects.nonNull(categoryDO)) {
            //处理分类已存在的逻辑
            log.warn("分类名称：{}，此分类已存在", categoryName);
            throw new BizException(ResponseCodeEnum.CATEGORY_NAME_IS_EXISTED);
        }

        //不存在则增加到数据库中
        CategoryDO addedCategory = CategoryDO.builder()
                .name(categoryName)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now()).build();

        int i = categoryMapper.insert(addedCategory);

        return i == 1 ? Response.success() : Response.fail();
    }


    public PageResponse getCategoryList(GetCategoryPageListReqVO findCategoryPageListReqVO) {
        // 获取当前页、以及每页需要展示的数据数量
        Long current = findCategoryPageListReqVO.getCurrent();
        Long size = findCategoryPageListReqVO.getSize();

        // 分页对象(查询第几页、每页多少数据)
        Page<CategoryDO> page = new Page<>(current, size);


        String name = findCategoryPageListReqVO.getName();
        LocalDate startDate = findCategoryPageListReqVO.getStartDate();
        LocalDate endDate = findCategoryPageListReqVO.getEndDate();



        // 执行分页查询
        Page<CategoryDO> categoryDOPage = categoryMapper.selectPageList(page, name,startDate,endDate);

        List<CategoryDO> categoryDOS = categoryDOPage.getRecords();

        // DO 转 VO
        List<FindCategoryPageListRspVO> vos = null;
        if (!CollectionUtils.isEmpty(categoryDOS)) {
            vos = categoryDOS.stream()
                    .map(categoryDO -> FindCategoryPageListRspVO.builder()
                            .id(categoryDO.getId())
                            .name(categoryDO.getName())
                            .createTime(categoryDO.getCreateTime())
                            .build())
                    .collect(Collectors.toList());
        }

        return PageResponse.success(categoryDOPage, vos);
    }

    /**
     * 删除分类接口
     *
     * @param deleteCategoryReqVO
     * @return
     */
    public Response deleteCategory(DeleteCategoryReqVO deleteCategoryReqVO) {
        Long id = deleteCategoryReqVO.getId();
        categoryMapper.deleteById(id);
        return Response.success();
    }


    public Response getCategorySelectList() {
        //获取到CaategoryDO列表
        List<CategoryDO> categoryDOList = categoryMapper.selectList(null);
        //将DO列表转为VO
        List<SelectRspVO> selectRspVOList = categoryDOList.stream().map(categoryDO -> {
            return SelectRspVO.builder()
                    .label(categoryDO.getName())
                    .value(categoryDO.getId())
                    .build();
        }).collect(Collectors.toList());
        return Response.success(selectRspVOList);
    }

}
